package cn.gzsxt.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gzsxt.po.PageOut;
import cn.gzsxt.po.ShowEcharts;
import cn.gzsxt.po.TaskDetail;
import cn.gzsxt.service.TaskService;
import cn.gzsxt.service.impl.TaskServiceImpl;

import com.alibaba.fastjson.JSON;

@WebServlet("/IssuedTask.oa")
public class IssuedTaskServlet extends HttpServlet {
	
	TaskService service = new TaskServiceImpl();
	PageOut page = new PageOut();
	static final ShowEcharts echart = new ShowEcharts();
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		String cmd = req.getParameter("cmd");
		if (cmd.equals("taskList")){
			getTask(req,resp);
		} else if (cmd.equals("kW")){
			byKeyWord(req,resp);
		} else if (cmd.equals("pS")){
			byPageSize(req,resp);
		} else if (cmd.equals("pM")){
			pageMove(req,resp);
		} else if (cmd.equals("getPage")){
			getPage(req,resp);
		} else if (cmd.equals("echarts")){
			showEcharts(req,resp);
		}
	}

	private void getPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		//传送分页信息
		PrintWriter out = resp.getWriter();
		out.print(JSON.toJSONString(page));
		out.flush();
		out.close();
	}

	private void byPageSize(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String pageSize = req.getParameter("pageSize");
		if (pageSize != null && Integer.valueOf(pageSize) != null){
			page.setPageSize(Integer.valueOf(pageSize));
			showPage(page,req,resp);
		}		
	}

	private void byKeyWord(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String keyWord = req.getParameter("keyWord");
		if (keyWord != null){
			page.setKeyword(URLDecoder.decode(keyWord,"UTF-8"));
			showPage(page,req,resp);
		}		
	}

	private void pageMove(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String move = req.getParameter("move");
		if (move != null && Integer.valueOf(move) != null){
			Integer moves = Integer.valueOf(move);
			if (moves == 1024*1024){
				page.setCurrentPage(page.getTotalPage());
			} else if (moves == -1024*1024){
				page.setCurrentPage(1);
			} else {
				Integer currentPage = page.getCurrentPage();
				//重新设置当前页
				page.setCurrentPage(currentPage+moves);
			}			
			showPage(page,req,resp);
		}		
	}

	private void getTask(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		page = new PageOut();
		page.setPageSize(10);
		page.setKeyword("");
		page.setCurrentPage(1);
		showPage(page,req,resp);
	}
	
	private void showPage(PageOut page, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PrintWriter out = resp.getWriter();
		List<TaskDetail> tasks = service.getSelectRows(page);
		page.setTotalCount(tasks.size());
		StringBuffer sb = new StringBuffer();	
		echart.setNormalCounts(0);
		echart.setRushCounts(0);
		echart.setSeriousCounts(0);
		for (TaskDetail task : tasks){
			sb.append("<tr id='task"+task.getTaskId()+"'>");
			sb.append("<td>"+task.getTaskId()+"</td>");
			sb.append("<td>"+task.getTaskTitle()+"</td>");
			sb.append("<td>"+task.getEndTime()+"</td>");
			sb.append("<td>"+task.getClerk()+"</td>");
			sb.append("<td>"+transStatus(task.getStatus())+"</td>");
			sb.append("<td class='"+getRushColor(task.getRushStatus())+"'>"+transRushStatus(task.getRushStatus())+"</td>");
			sb.append("<td width='5%' class='mian_b_icon_01'><a class='label label-primary' onclick='checkDetail("+task.getTaskId()+")'>任务详情</a></td>");
			sb.append("<td width='5%' class='mian_b_icon_01'><a class='label label-warning' onclick='applyDelay("+task.getTaskId()+")'>申请延期</a></td>");
			sb.append("</tr>");
		}
		out.print(sb.toString());
		out.flush();
		out.close();
	}

	private String getRushColor(Integer rush) {
		switch (rush){
		case 1:
			return "text-warning";
		case 2:
			return "text-danger";
		default:
			return "text-navy";
		}
	}

	private void showEcharts(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		resp.getWriter().print(JSON.toJSONString(echart));
	}
	
	private String transRushStatus(Integer rush){
		switch (rush){
		case 1:
			echart.addNormalCounts();
			return "紧急";
		case 2:
			echart.addRushCounts();
			return "重大";
		default:
			echart.addSeriousCounts();
			return "一般";
		}
	}

	private String transStatus(Integer status){
		switch (status){
		case 1:
			return "同意";
		case 2:
			return "拒绝";
		default:
			return "审核中";
		}
	}
}
